<template>
  <a-layout-header class="header">
    <a-popconfirm
        title="确认退出登陆?"
        ok-text="确定"
        cancel-text="取消"
        @confirm="logout"
    >
      <a class="login-menu" v-show="!!user.id">
        <span>退出</span>
      </a>
    </a-popconfirm>
    <span v-show="!!user.id" class="login-menu">你好，{{ user.name }}</span>
    <a class="login-menu" @click="showLoginModal" v-show="!user.id">
      <span>登录</span>
    </a>
    <div class="logo">OliverBook</div>
    <a-menu
        theme="dark"
        mode="horizontal"
        :style="{ lineHeight: '64px' }"
    >
      <a-menu-item key="/">
        <router-link to="/">首页</router-link>
      </a-menu-item>
      <a-menu-item key="/about" style="order: 5;">
        <router-link to="/about">关于本站</router-link>
      </a-menu-item>
      <a-menu-item key="/admin/user" :style="!!user.id? {} : {display:'none'}">
        <router-link to="/admin/user">用户管理</router-link>
      </a-menu-item>
      <a-menu-item key="/admin/ebook" :style="!!user.id? {} : {display:'none'}">
        <router-link to="/admin/ebook">电子书管理</router-link>
      </a-menu-item>
      <a-menu-item key="/admin/category" :style="!!user.id? {} : {display:'none'}">
        <router-link to="/admin/category">分类管理</router-link>
      </a-menu-item>

    </a-menu>
    <a-modal title="用户登录"
             v-model:visible="loginModalVisible"
             :confirm-loading="loginModalLoading"
             @ok="login">
      <a-form :model="loginUser" :label-col="{ span : 6}" :wrapper-col="wrapperCol">
        <a-form-item label="用户名">
          <a-input v-model:value="loginUser.loginName"/>
        </a-form-item>
        <a-form-item label="密码">
          <a-input v-model:value="loginUser.password" type="password"/>
        </a-form-item>
      </a-form>
    </a-modal>
  </a-layout-header>
</template>

<script lang="ts">
import {defineComponent, ref, computed} from "vue";
import axios from "axios";
import {message} from "ant-design-vue";
import store from "@/store";

declare let hexMd5: any;
declare let KEY: any;

export default defineComponent({
  name: "TheHeader",
  setup() {
    // 用来保存登录后信息的对象
    const user = computed(() => store.state.user);
    // 用来保存登录信息的对象
    const loginUser = ref({
      loginName: "test",
      password: "test"
    });
    const loginModalVisible = ref(false);
    const loginModalLoading = ref(false);
    const showLoginModal = () => {
      loginModalVisible.value = true;
    };
    // 登陆方法
    const login = () => {
      console.log("登录中！！")
      loginModalLoading.value = true;
      // 用户密码加盐MD5
      loginUser.value.password = hexMd5(loginUser.value.password + KEY);
      // 登录
      axios.post("/user/login", loginUser.value).then((response) => {
        loginModalLoading.value = false;
        const data = response.data;
        if (data.success) {
          loginModalVisible.value = false;
          message.success("登录成功")
          // 保存用户信息至vuex全局变量中
          store.commit("setUser", data.content);
        } else {
          message.error(data.message)
        }
      })
    };

    // 退出登陆方法
    const logout = () => {
      console.log("退出登陆中~~")
      axios.get("/user/logout/" + user.value.token).then((response) => {
        const data = response.data;
        if (data.success) {
          message.success("退出登陆成功")
          // 保存用户信息至vuex全局变量中
          store.commit("setUser", {});
        } else {
          message.error(data.message)
        }
      })
    };
    return {
      loginUser,
      loginModalVisible,
      loginModalLoading,
      showLoginModal,
      login,
      user,
      logout,

    }
  }
})
</script>

<style>
.logo {
  width: 120px;
  height: 31px;
  float: left;
  color: white;
  font-size: 18px;
}

.login-menu {
  color: white;
  float: right;
  padding-left: 10px;
  font-size: 15px;
}
</style>